/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSColor.h"
#include "NSData.h"
#include "NSKeyedUnarchiver.h"

IMPLEMENT_DYNAMIC_CLASS(NSColor, NSObject)

void NSColor::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSObject::initWithUnarchiver(archiver);
	long colorspace = archiver->decodeIntForKey(wxT("NSColorSpace"));
	switch(colorspace)
	{
	case 1:  // calibrated rgb
		{
			NSData *data = archiver->decodeDataForKey(wxT("NSRGB"));
			wxString str((const wxChar*)data->getData(), wxConvUTF8, data->length());
			size_t pos = str.find(' ');
			wxASSERT(pos != wxString::npos);
			wxString sRed = str.substr(0, pos);
			pos++;
			size_t pos2 = str.find(' ', pos);
			wxASSERT(pos2 != wxString::npos);
			wxString sGreen = str.substr(pos, pos2-pos);
			wxString sBlue = str.substr(pos2+1);
			double red, green, blue;
			bool ret = sRed.ToDouble(&red) && sGreen.ToDouble(&green) && sBlue.ToDouble(&blue);
			wxASSERT(ret);
		}
		break;
	case 3:  // calibrated white
		{
			NSData *data = archiver->decodeDataForKey(wxT("NSWhite"));
			wxString str((const wxChar*)data->getData(), wxConvUTF8, data->length());
			double white, alpha = 1.0;
			size_t pos = str.find(' ');
			if (pos != wxString::npos)
			{
				wxString str_alpha(str.substr(pos+1));
				bool ret = str_alpha.ToDouble(&alpha);
				wxASSERT(ret);
				str.Truncate(pos);
			}
			bool ret = str.ToDouble(&white);
			wxASSERT(ret);
		}
		break;
	case 6:
		{
			wxString catalog = archiver->decodeStringForKey(wxT("NSCatalogName"));
			wxString name = archiver->decodeStringForKey(wxT("NSColorName"));
		}
		break;
	default:
		wxASSERT(false);
	}
}